package se.haaga.jsupervision.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import se.haaga.jsupervision.gui.MainWindow;
import se.haaga.jsupervision.gui.linux.LinuxMonitorTab;
import se.haaga.jsupervision.monitor.linux.LinuxMonitor;
import se.haaga.jsupervision.monitor.linux.LinuxMonitors;
import se.haaga.jsupervision.util.ActionConstants;
import se.haaga.jsupervision.util.Translator;

public class JUbuntuActionListener implements ActionListener {

	private final MainWindow mainWindow;

	public JUbuntuActionListener(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(ActionConstants.EXIT.toString())) {
			mainWindow.confirmExit();
		} else if (e.getActionCommand().equals(ActionConstants.ADD_LINUX_CONNECTION_TAB.toString())) {			
			JMenuItem jmi = (JMenuItem) e.getSource();
			String text = jmi.getText();
			if (!mainWindow.isTabOpen(text)) {
				LinuxMonitor linuxMonitor = LinuxMonitors.getInstance().getLinuxMonitor(text);
				linuxMonitor.openConnection();
				LinuxMonitorTab linuxMonitorTab = new LinuxMonitorTab(text);
				mainWindow.addToTabPane(text, linuxMonitorTab);
			}
		} else if (e.getActionCommand().equals(ActionConstants.CLOSE_TAB.toString())) {
			mainWindow.closeTabPane();
		} else if (e.getActionCommand().equals(ActionConstants.ABOUT.toString())) {
			JOptionPane.showMessageDialog(mainWindow, Translator.ABOUT_MESSAGE.getTranslation() + Translator.VERSION.getTranslation(),
			        Translator.MAIN_PROGRAM_TITLE.getTranslation(), JOptionPane.INFORMATION_MESSAGE, 
			        null); // FIXME Set a icon..
		} else {
			System.err.println(e.getActionCommand() + " is not mapped in action handler!");
		}
	}

}
